/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "AI_Manager.h"

#include "EntityManager.h"
#include "IEntity.h"


AI_Manager::AI_Manager(void)
{
    Register();
	Init();
}

AI_Manager::~AI_Manager(void)
{
    UnRegister();
}

/** Add Brains */
void AI_Manager::Init()
{
	m_brainCollection.push_front( &m_recklessBrain );
}


/** Add an entity, will find proper brain */
void AI_Manager::AddEntity(IEntity* entPtr)
{
	type_BrainList::iterator iter;
	for(iter = m_brainCollection.begin(); iter != m_brainCollection.end(); ++iter)
	{
		if ( entPtr->m_attributes.Has( "ai_type" ))
		{
			if(entPtr->m_attributes.GetInt("ai_type") == (*iter)->GetBrainType())
			{
				(*iter)->AddEntity(entPtr);
				break;
			}
		}
	}
}

/** Remove an entity from a brain */
void AI_Manager::RemoveEntity(IEntity* entPtr)
{
	type_BrainList::iterator iter;
	int aiType;

	if(entPtr->m_attributes.Has("ai_type"))
	{
		aiType = entPtr->m_attributes.GetInt("ai_type");
		for(iter = m_brainCollection.begin(); iter != m_brainCollection.end(); ++iter)
		{
            if((*iter)->GetBrainType()== aiType) {
				(*iter)->RemoveEntity(entPtr);
                break;
            }
		}

	}
	
}

/** Rm a brain */
void AI_Manager::RemoveBrain( AI_Brain* brain )
{
    type_BrainList::iterator it = m_brainCollection.begin();
    for(; it != m_brainCollection.end(); ++it )
    {
        if ( (*it) == brain )
        {
            brain->RemoveAll(); // remove all contents
            m_brainCollection.erase( it );
            return;
        }
    }
}

/** Update each brain in term updating each entity's think */
void AI_Manager::UpdateBrains()
{
	type_BrainList::iterator iter;
	for(iter = m_brainCollection.begin(); iter != m_brainCollection.end(); ++iter)
	{
		(*iter)->ControlEntities();
	}
}


/** Handle an event */
bool AI_Manager::HandleEvent( IEvent* e )
{
	bool success = false;
	switch( e->GetType() )
	{
	case EVT_ADD_ENTITY:
		AddEntity( e->GetData<EntityAdditionData>()->m_entity );
		success = true;
		break;
	case EVT_RM_ENTITY:
		RemoveEntity( e->GetData<EntityAdditionData>()->m_entity );
		success = true;
		break;
	default:
		std::cout << "AI_Manager:: Could not handle event type: " << e->GetType() << "\n";
		break;
	}
	return success;

}